// pages/examhome/examhome.js
var app = getApp();
Page({

  data: {
    passf: 0,
    time: 0,
    num: 0,
    everyf: 1,
    use_title: "",
    training_qids: ""
  },

  onLoad: function (options) {

    this.setData({
      exam: options.exam
    })
    this.onQuery(options.exam);
    this.get(options.exam);
  },
  getRandomArrayElements(arr,count){
    var shuffled = arr.slice(0),
      i = arr.length,
      min = i - count,
      temp,index;
    while ( i-- > min){
      index = Math.floor((i + 1) * Math.random());
      temp = shuffled[index];
      shuffled[index] = shuffled[i];
      shuffled[i] = temp;
    }

    return shuffled.slice(min);
  },

  get: function(examid){
    // 调用云函数
    let arr = [];
    wx.cloud.callFunction({
      name: 'getbytype',
      data: {
        examid: examid
      },
      success: res => {
            console.log('[云函数] [getbytype]: ', res)
            let arr1 = [];
            res.result.data1.forEach((it)=>{
              arr1.push(it._id);
            })
            let arr2 = [];
            res.result.data2.forEach((it)=>{
              arr2.push(it._id);
            })
            let arr3 = [];
            res.result.data3.forEach((it)=>{
              arr3.push(it._id);
            })
        
            let newarr1 = [];
            let newarr2 = [];
            let newarr3 = [];
              if(examid == '001'){
              newarr1 = this.getRandomArrayElements(arr1,60);
              newarr3 = this.getRandomArrayElements(arr3,40);
            }
            if(examid == '002'){
              newarr1 = this.getRandomArrayElements(arr1,20);
              newarr2 = this.getRandomArrayElements(arr2,10);
              newarr3 = this.getRandomArrayElements(arr3,20);
            }
            

            arr = newarr1.concat(newarr2,newarr3);
            console.log('组题')
            console.log(arr);
            try {
              wx.setStorageSync('arr', arr)
            } catch (e) { }
          },
          fail: err => {
            console.error('[云函数] [login] 调用失败', err)
            wx.navigateTo({
              url: '../deployFunctions/deployFunctions',
            })
          }
        })
  },
  onQuery: function(exam) {
    let that = this;

    const db = wx.cloud.database()
    db.collection('exams').doc(exam).get().then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let exam = res.data;
      let unit = exam.total/exam.num;
      that.setData({
        exam: exam,
        unit: unit
      });
    })
  },
  onReady: function () {

  },

  onShow: function () {

  },
  onGotUserInfo: function(e) {
    let that = this;
    console.log(e);
    if (e.detail.errMsg === 'getUserInfo:fail auth deny') {
        //拒绝
        wx.showModal({
          showCancel: false,
          title: '提示',
          confirmText: '我知道了',
          content: '为了更好的体验，请您先通过',
          success (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })        

    } else if (e.detail.errMsg === 'getUserInfo:ok') {
        //允许
        
        let openid = this.data.openid;
        let userInfo = e.detail.userInfo;

        const db = wx.cloud.database();
        db.collection('user').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            userInfo: userInfo
          },
          success: function(res) {
            console.log('[数据库] [查询记录] 成功: ', res);
            that.examGo();
          }
        })

    }

  },
  examGo:function (e) {
    let time = this.data.time;
    let passf = this.data.passf;
    let num = this.data.num;

    wx.redirectTo({
      url: "../exam/exam?exam="+this.data.exam._id+"&continued=1&passf=" + passf + "&time=" + time + "&training_qids=0&nums=" + num
    });
  }
})